package com.litong.litongjiaoyu.utils;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;
import java.sql.Time;

/**
 * @ClassName SqlTimeDeserializer
 * @Description 自定义 {@link java.sql.Time} 时间类型反序列化规则.
 * @Author jingz
 * @Date 2020/2/21 16:36
 * @Version 1.0
 **/
public class SqlTimeDeserializer implements ObjectDeserializer {
    @SuppressWarnings("unchecked")
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        Object val = parser.parse();
        if(val != null){
            //格式为 HH:mm:ss
            String timeStr = val.toString();
            if( timeStr.indexOf(":") == 2 && timeStr.length() == 5 ){

                return (T) Time.valueOf(val + ":00");
            }
        }
        return null;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
